﻿using UnityEngine;
using System.Collections;
using NhomTre_XocXoc;
using System.Threading;
public class XocDiaController : MonoBehaviour {

	private BonNut bonnut= new BonNut();
	public static Nut[] nut;
	public GameObject NutDo;
	public GameObject NutDen;
	public Texture btnTexture;
	Vector3[] position = new Vector3[4];
	public Animator animator;
	private float time=60f;
	public Object[] nutmoi;
	public static bool chophepdat;
	public static bool bdvanmoi;
	public static bool modia;
	public GUIText textThongBao;
	public static long tien;
	public static bool baokq;
	// Use this for initialization
	void Start () {
		position[0].Set(-0.2536342f,0.5724221f,0f);
		position[1].Set(0.3446883f,00.6058814f,0f);
		position[2].Set(0.2876917f,0.05526927f,0f);
		position[3].Set(-0.2886653f,0.01715386f,0f);
		animator = gameObject.GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void Update () {
		if (time>0f) {
			time-=Time.deltaTime;
			baokq=false;
		}
		if (time<60f) {
			if (time<60f && time>50f) {
				HuyNut();

			}
			Up();
		}
		if (time<45) {

			Xoc();
		}
		if (time<38) {
			if(time <38f && time>37.985f){	
				TaoNut();
			}
			Mo();
			baokq=true;
		}
		if (time < 35) {
			Hien();}
		   
		if (time<20) {
			time=60f;
		}
		if (time<56 && time >45) {
			chophepdat=true;
			}
		if (time<45 ) {
			chophepdat = false;
				}
		if (time < 60 && time > 58) {
						bdvanmoi = true;
				} else
						bdvanmoi = false;
		if (time < 38 && time > 37.995) {
						modia = true;
		
				} else
						modia = false;
		ThongBao ();
	}
	void TaoNut()
	{   nutmoi = new Object[4];
		nut =  new Nut[4];
		nut = bonnut.XocDia();
		for (int i = 0; i < 4; i++) {
			
			if (nut[i].Mau==0) {
			nutmoi[i]=	Instantiate(NutDo,position[i],transform.rotation);	
			}
			if (nut[i].Mau==1) {
			nutmoi[i]=	Instantiate(NutDen,position[i],transform.rotation);	
			}
			
		}
	}

	void ThongBao()
	{
		string chuoi = "";
		if (time<60f) {
			chuoi="Chuan bi van moi";
		}
		if (time<55f) {
			chuoi="Dat cua di ba con";
		}
		if (time<46) {
			chuoi="Xoc ne";
		}
		if (time<39) {
			chuoi="Mo nao";
		}
		if (time<37) {
			chuoi="Thanh toan,thanh toan";
		}
		textThongBao.text=chuoi;
	}
	void HuyNut()
	{
		for (int i = 0; i < 4; i++) {
			Destroy(nutmoi[i]);
		}
	}
	void Up()
	{
		animator.SetBool ("isUp", true);
		animator.SetBool ("isHien", false);
	}
	void Xoc()
	{

		animator.SetBool ("isUp", false);
		animator.SetBool ("isXoc", true);
	}
	void Mo()
	{
		animator.SetBool ("isMo", true);
		animator.SetBool ("isXoc", false);
	}
	void Hien()
	{
		animator.SetBool ("isHien", true);
		animator.SetBool ("isMo", false);
	}
	void OnGUI()
	{
		if( GUI.Button(new Rect (20,20, 70, 25), "Quay Lai")){
			Application.LoadLevel("Login");
		}
	}
}
